package com.ckjw.gobang.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.ckjw.gobang.connect.UserClientService;

/**
 * 注册页面
 * @author lck and djw
 * @Date 2022年6月12日-下午3:44:27
 */
public class register {
    static JFrame regist = new JFrame("注册页");
    final static int wide = 500;
    final static int high = 600;


    //初始话注册页面
    public static void init() throws IOException {
        double w = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        double h = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        regist.setBounds((int) (w - wide) / 2, (int) (h - high) / 2, wide, high);
        regist.setLayout(null);
        //添加布局
        Box vbox = Box.createVerticalBox();
        vbox.setBounds(110,50,280,350);
        //第一行布局
        Box ubox = Box.createHorizontalBox();
        JLabel ulabel = new JLabel("用户名称:");
        Font ufont = new Font("宋体", Font.BOLD + Font.ITALIC, 20);
        ulabel.setFont(ufont);
        //ulabel.setForeground(Color.red);
        JTextField utxt = new JTextField(15);
        utxt.setSelectedTextColor(Color.red);
        ubox.add(ulabel);
        ubox.add(Box.createHorizontalStrut(20));
        ubox.add(utxt);
        //第二行布局
        Box zbox = Box.createHorizontalBox();
        JLabel zlabel = new JLabel("设置账号:");
        zlabel.setFont(ufont);
        //zlabel.setForeground(Color.red);
        JTextField ztxt = new JTextField(15);
        ztxt.setSelectedTextColor(Color.red);
        zbox.add(zlabel);
        zbox.add(Box.createHorizontalStrut(20));
        zbox.add(ztxt);

        //第三行布局
        Box pbox = Box.createHorizontalBox();
        JLabel plabel = new JLabel("设置密码:");
        plabel.setFont(ufont);
        //plabel.setForeground(Color.red);
        JPasswordField jtxt = new JPasswordField(15);
        jtxt.setSelectedTextColor(Color.red);
        pbox.add(plabel);
        pbox.add(Box.createHorizontalStrut(20));
        pbox.add(jtxt);

        //第四行布局
        Box bbox = Box.createHorizontalBox();
        Font bfont = new Font("宋体", Font.BOLD + Font.ITALIC, 18);
        JButton rbutton = new JButton("注册");
        rbutton.setFont(bfont);
        JButton fbutton = new JButton("返回登陆页面");
        fbutton.setFont(bfont);
        bbox.add(rbutton);
        bbox.add(Box.createHorizontalStrut(60));
        bbox.add(fbutton);
        //输入图书馆名称回车  监听
        utxt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ztxt.requestFocus();
            }
        });
        //输入账号回车  监听
        ztxt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jtxt.requestFocus();
            }
        });
        //输入密码回车  监听
        jtxt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rbutton.doClick();
            }
        });
        //鼠标点击注册 监听
        rbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String usr = utxt.getText().trim();
                String num = ztxt.getText().trim();
                String pas = jtxt.getText().trim();
                if (new UserClientService().regist(num, usr, pas)) {
                	JOptionPane.showMessageDialog(MainInterface.mainInterface, "注册成功", "提示消息",
    						JOptionPane.INFORMATION_MESSAGE);
                	regist.dispose();
				}
                else {
					System.out.println("注册失败");
					JOptionPane.showMessageDialog(MainInterface.mainInterface, "注册失败", "提示消息",
							JOptionPane.WARNING_MESSAGE);
				}
            }    
        });

        //鼠标点击返回监听
        fbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                regist.dispose();
                try {
					new login().init();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
        });

        //行布局添加到列布局
        vbox.add(Box.createVerticalStrut(100));
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(30));
        vbox.add(zbox);
        vbox.add(Box.createVerticalStrut(30));
        vbox.add(pbox);
        vbox.add(Box.createVerticalStrut(30));
        vbox.add(bbox);

        regist.add(vbox);
        regist.setResizable(false);
        regist.setVisible(true);
    }
}
